package com.darrenchan.leetcode;

/**
 * @Desc
 * @Author chenchi03
 * @CreateTime 2019-12-26 13:15
 */
class ListNode {
    int val;
    ListNode next;

    ListNode(int x) {
        val = x;
    }
}

public class Q19 {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode dummy = new ListNode(0);//虚拟头结点
        dummy.next = head;
        ListNode slow = dummy, fast = dummy;
        int times = 0;
        while (fast != null) {
            if(times > n) {
                slow = slow.next;
            }
            times++;
            fast = fast.next;
        }
        slow.next = slow.next.next;
        return dummy.next;
    }
}
